package com.game.objects;

import com.game.system.Utils;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.input.MouseInput;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Quad;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleGeometry;

public class Player{
	private Node player;
	private Quad[] cross = new Quad[2];
	private Sphere[] bulletPos;
	private Vector3f[] bulletPositions;
	private ParticleGeometry exhaust;
	private Quaternion p90;
	private Quaternion n90;
	private Quaternion rotation;
	private final float speed = 0.6f;
	private final float tiltingSensivity = 0.5f *40;
	private Vector3f cursorPos = new Vector3f();
	Camera cam;
	public Player(){
		cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();
		player = new Node("Player");
		player = (Node)Utils.loadModel("jet");
		bulletPos = new Sphere[2];
		bulletPositions = new Vector3f[2];
		player.setModelBound(new BoundingBox());
		player.updateModelBound();
        for(int i=0; i<bulletPos.length; i++){
        	bulletPos[i] = new Sphere(""+i, 10, 10, 0.0001f);
        	bulletPos[i].setModelBound(new BoundingSphere());
        	bulletPos[i].updateModelBound();
        	player.attachChild(bulletPos[i]);
        	bulletPositions[i] = bulletPos[i].getWorldTranslation();
        }
    	bulletPos[0].setLocalTranslation(-1.5f, 0.5f, 6.1f);
    	bulletPos[1].setLocalTranslation(1.5f, 0.5f, 6.1f);
    	
        AlphaState as = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        as.setBlendEnabled(true); 
        as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
        as.setTestEnabled(false); 
        as.setEnabled(true);

    	for(int i=0; i < cross.length;i++){
    		cross[i] = new Quad("Cursor"+i, 4, 4);
    		cross[i].setModelBound(new BoundingBox());
    		cross[i].updateModelBound();
    		cross[i].setRenderState(as);
    		cross[i].setRenderState(Utils.createTextureState("cross"));
    		cross[i].setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
    	}
		p90 = new Quaternion();
		n90 = new Quaternion();
		p90.fromAngleAxis(FastMath.DEG_TO_RAD*90, Vector3f.UNIT_Z);
		n90.fromAngleAxis(FastMath.DEG_TO_RAD*-90, Vector3f.UNIT_Z);
		rotation = new Quaternion();
		
        AlphaState particleAlpha = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        particleAlpha.setBlendEnabled(true);
        particleAlpha.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        particleAlpha.setTestEnabled(true);
        particleAlpha.setTestFunction(AlphaState.TF_GREATER);
        particleAlpha.setDstFunction(AlphaState.DB_ONE);
        
        ZBufferState zstate = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
        zstate.setEnabled(false);
        
		exhaust = ParticleFactory.buildParticles("Exhaust",300);
        exhaust.setEmissionDirection(Vector3f.UNIT_Z);
        exhaust.setMaximumAngle(FastMath.DEG_TO_RAD*2);
        exhaust.setMinimumAngle(0);
        exhaust.getParticleController().setSpeed(1.5f);
        exhaust.setMinimumLifeTime(50);
        exhaust.setMaximumLifeTime(100);
        exhaust.setStartSize(0.45f);
        exhaust.setEndSize(0.90f);
        exhaust.setStartColor(new ColorRGBA(1, 1, 1, 1));
        exhaust.setEndColor(new ColorRGBA(0.6f, 0.3f, 0, 0));
        exhaust.getParticleController().setControlFlow(false);
        exhaust.setReleaseRate(300);
        exhaust.setReleaseVariance(0);
        exhaust.setInitialVelocity(0.015f);
        exhaust.getParticleController().setRepeatType(Controller.RT_WRAP);
        exhaust.warmUp(60);
        exhaust.setRenderState(Utils.createTextureState("exhaust"));
        exhaust.setRenderState(particleAlpha);
        exhaust.setRenderState(zstate);
        exhaust.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
        exhaust.updateRenderState();
        exhaust.setOriginOffset(new Vector3f(0, 1.2f, -0.8f));
        //exhaust.setLocalTranslation(0, 1.2f, -1);
		player.attachChild(exhaust);
	}
	public Node getPlayer(){
		return player;
	}
	public Quad[] getCross(){
		return cross;
	}

	public ParticleGeometry getParticles(){
		return exhaust;
	}
	public void update(){
		
		Vector2f mousePos = new Vector2f(MouseInput.get().getXAbsolute(), 
				MouseInput.get().getYAbsolute());
		
		cursorPos.x=mousePos.x/DisplaySystem.getDisplaySystem().getWidth()*40-20;
		cursorPos.y=mousePos.y/DisplaySystem.getDisplaySystem().getHeight()*30-15;
		
		cursorPos.addLocal(player.getLocalTranslation().mult(0.9f));

		cursorPos.z=-15;
		cross[0].setLocalTranslation(cursorPos);
		cross[1].setLocalTranslation(cursorPos.subtract(player.getLocalTranslation()).add(cursorPos));
		cross[1].getLocalTranslation().z=-30;
		
		player.lookAt(cursorPos, Vector3f.UNIT_Y);
		float distX = ((cursorPos.x-player.getLocalTranslation().x)/tiltingSensivity+1)/2;
		rotation.slerp(n90, p90, distX);
		player.setLocalRotation(player.getLocalRotation().mult(rotation));
		
		
		if(cursorPos.x-player.getLocalTranslation().x>speed*1) 
			player.getLocalTranslation().x+=speed;
		else if(cursorPos.x-player.getLocalTranslation().x<speed*-1) 
			player.getLocalTranslation().x-=speed;
		else player.getLocalTranslation().x+=cursorPos.x-player.getLocalTranslation().x;
		
		if(cursorPos.y-player.getLocalTranslation().y>speed*1) 
			player.getLocalTranslation().y+=speed;
		else if(cursorPos.y-player.getLocalTranslation().y<speed*-1) 
			player.getLocalTranslation().y-=speed;
		else player.getLocalTranslation().y+=cursorPos.y-player.getLocalTranslation().y;
		

		if(player.getLocalTranslation().x>=90){
			player.getLocalTranslation().x=90;
		}else if(player.getLocalTranslation().x<=-90){
			player.getLocalTranslation().x=-90;
		}
		
		if(player.getLocalTranslation().y>=30){
			player.getLocalTranslation().y=30;
		}else if(player.getLocalTranslation().y<=-30){
			player.getLocalTranslation().y=-30;
		}
		
		Vector3f camLoc = player.getLocalTranslation().divide(new Vector3f(1.3f,1.75f,1f));
		camLoc.setZ(cam.getLocation().getZ());
		cam.setUp(Vector3f.UNIT_Y.add(new Vector3f(player.getLocalTranslation().x,0f,0f).mult(0.002f)));
		cam.setLocation(camLoc);
		cam.update();
	}
	public Vector3f getBulletPosition(int index){
		return bulletPositions[index];
	}
	
	public void collision() {
		//System.out.println("booooooom");
	}
	
}